package com.qkm.xinguan.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qkm.xinguan.domain.entity.Complaint;
import com.qkm.xinguan.domain.entity.HouseInfo;
import com.qkm.xinguan.domain.infrastructure.mapper.ComplaintMapper;
import com.qkm.xinguan.exception.BusinessException;
import com.qkm.xinguan.repository.ComplaintRepository;
import com.qkm.xinguan.repository.TouristRepository;
import com.qkm.xinguan.repository.UserRepository;
import com.qkm.xinguan.response.Result;
import com.qkm.xinguan.response.ResultCode;
import com.qkm.xinguan.vo.ComplaintVo;
import com.qkm.xinguan.vo.HouseInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;


@Service
public class ComplaintRepositoryImpl extends ServiceImpl<ComplaintMapper, Complaint> implements ComplaintRepository {

	private final TouristRepository touristRepository;
	private final UserRepository userRepository;

	@Autowired
	public ComplaintRepositoryImpl(TouristRepository touristRepository, UserRepository userRepository) {
		this.touristRepository = touristRepository;
		this.userRepository = userRepository;
	}

	@Override
	public int updateDeal(Long id){
		return this.baseMapper.updateDeal(id);
	}

	/**
	 * 分页获取投诉信息列表
	 *
	 * @param page 页数
	 * @param size 条数
	 * @return Page<ComplaintVo>
	 */
	@Override
	public Page<ComplaintVo> getComplaintListPage(Integer page, Integer size) {
		LambdaQueryWrapper<Complaint> wrapper=new LambdaQueryWrapper <>();
		wrapper.ne(Complaint::getDeal,1);
		Page<Complaint> p= this.baseMapper.selectPage(new Page<>(page, size), wrapper);
		return toComplaintVoPage(p);
	}

	@Override
	public Result getAllListById(Long id) {
			List<Complaint> complaints=this.baseMapper.getComplaintById(id);
			return Result.ok().data(complaints) ;

	}

	@Override
	public Result deleteById(Long id) {
		int num = this.baseMapper.deleteById(id);
		if(num>0){
			return Result.ok().message("删除成功");
		}
		throw new BusinessException(ResultCode.COMPLAINT_CANCEL.getCode(), "投诉撤消失败");
	}

	private Page<ComplaintVo> toComplaintVoPage(Page<Complaint> complaintPage) {
		Page<ComplaintVo> pageInfo = new Page<>();
		BeanUtils.copyProperties(complaintPage, pageInfo);
		List<ComplaintVo> complaintVos = toComplaintVoList(complaintPage.getRecords());
		pageInfo.setRecords(complaintVos);
		return pageInfo;
	}

	private List<ComplaintVo> toComplaintVoList(List<Complaint> complaints) {
		if (complaints.isEmpty()) {
			return Collections.emptyList();
		}
		List<ComplaintVo> complaintVos = new ArrayList<>();
		List<Long> userIds = complaints.stream().map(Complaint::getUserId).collect(Collectors.toList());
		List<Long> tourists = complaints.stream().map(Complaint::getTouristId).collect(Collectors.toList());
		// 根据 游客id查询游客名字
		List<String> userNames = userRepository.getUsersNameByIds(userIds);
		List<String> touristNames = touristRepository.getTouristNamesByIds(tourists);
		List<String> userPhones=userRepository.getUserPhonesByIds(userIds);
		List<String> touristPhones=touristRepository.getTouristPhonesByIds(tourists);
		AtomicInteger index = new AtomicInteger();
		AtomicInteger index1= new AtomicInteger();
		AtomicInteger index2= new AtomicInteger();
		AtomicInteger index3= new AtomicInteger();
		complaints.stream().map(complaint -> executeVo(complaint, userNames.get(index.getAndIncrement()), touristNames.get(index1.getAndIncrement()), userPhones.get(index2.getAndIncrement()), touristPhones.get(index3.getAndIncrement()))).forEach(complaintVo -> complaintVos.add(complaintVo));
		return complaintVos;
	}
	/**
	 * 构建 HouseInfoVo 对象
	 *
	 * @param complaint 房屋信息 Entity
	 * @param userName
	 * @return houseinfoVo
	 */
	public static ComplaintVo executeVo(Complaint complaint, String userName,String username1,String userPhone,String touristPhone) {
		ComplaintVo complaintVo = new ComplaintVo();
		BeanUtils.copyProperties(complaint, complaintVo);
		complaintVo.setUserName(userName);
		complaintVo.setTouristName(username1);
		complaintVo.setPhoneNumber(userPhone);
		complaintVo.setPhone(touristPhone);
		return complaintVo;
	}
}
